/*
 * KeyStrokeSender.java
 *
 * Created on January 30, 2007, 6:02 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package net.sf.iSnake.core;

import net.sf.iSnake.configuration.iSnakeConfiguration;

/**
 *
 * @author Jitendra
 */
public class KeyStrokeSender extends Thread {
    
    iSnakeConfiguration conf;
    ClientNetworkInterface clientNetworkInterface;
    GameController gameController;
    
    KeyboardInputHandler kih;
    
    postmodeISCPpacketForSend postmodeSend;
    
    private boolean startTrappingDirectionKeys;
    private boolean stopKeyboardInputHandler;
    private boolean sendKeystroke;

    /**
     * Creates a new instance of KeyStrokeSender
     */
    public KeyStrokeSender(iSnakeConfiguration conf,ClientNetworkInterface cni, GameController gc,KeyboardInputHandler kih) {    
        this.conf = conf;
        this.clientNetworkInterface = cni;
        this.gameController = gc;
        this.kih = kih;
        postmodeSend = new postmodeISCPpacketForSend();
    }
    
    /**
     * This will start trapping the input keystokes and send it to the game server and then enter wait mode until thread is woken up again
     */
    public synchronized void run() {
        System.out.println("keyboard input handler thread started");
        while(!isStopKeyboardInputHandler())    {
            if(isStartTrappingDirectionKeys() && isSendKeystroke())  {
//                System.out.println("checking for dx dy change");
                if(kih.isDxdyChanged()) {
                    System.out.println("sending changed keystroke to server: dx="+kih.getDxForSending()+" dy="+kih.getDyForSending());
                    postmodeSend.setPlayer_id(kih.getPlayer_id());
                    postmodeSend.setPostmodeCommandSend(ISCP.POST_CMD_SEND_dxdy);
                    postmodeSend.setDxDy(kih.getDxForSending(),kih.getDyForSending());
//                    postmodeInputKeysPacket.setDxDy(new Short("1"),new Short("1"));
                    clientNetworkInterface.send(postmodeSend);
                    System.out.println("sent values of dx= "+postmodeSend.getDx()+", dy= "+postmodeSend.getDy());
                }
                // do nothing if the input keystokes have not changed
                setSendKeystroke(false);
            }
            else    {
                try {
                    System.out.println("keyboard input handler waiting");    
                    wait();
                    System.out.println("keyboard input handler NOT waiting");
                } catch (InterruptedException ex) {
                        ex.printStackTrace();
                }
            }
        }
    }

    public synchronized void myNotifier()   {
        notifyAll();
    }
    public boolean isStartTrappingDirectionKeys() {
        return startTrappingDirectionKeys;
    }

    public void setStartTrappingDirectionKeys(boolean startTrappingDirectionKeys) {
        this.startTrappingDirectionKeys = startTrappingDirectionKeys;
    }

    public boolean isStopKeyboardInputHandler() {
        return stopKeyboardInputHandler;
    }

    public void setStopKeyboardInputHandler(boolean stopKeyboardInputHandler) {
        this.stopKeyboardInputHandler = stopKeyboardInputHandler;
    }

    public boolean isSendKeystroke() {
        return sendKeystroke;
    }

    public void setSendKeystroke(boolean sendKeystroke) {
        this.sendKeystroke = sendKeystroke;
    }
}
